home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / updatedb.notslocate < prev    next >
Encoding:
Text File  |  2007-03-05  |  8.5 KB  |  318 lines

  1. #! /bin/sh
  2. # updatedb -- build a locate pathname database
  3. # Copyright (C) 1994, 1996, 1997, 2000, 2001, 2003, 2004, 2005, 2006
  4. # Free Software Foundation, Inc.
  5.  
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2, or (at your option)
  9. # any later version.
  10.  
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15.  
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  19. # USA.
  20.  
  21. # csh original by James Woods; sh conversion by David MacKenzie.
  22.  
  23. usage="\
  24. Usage: $0 [--findoptions='-option1 -option2...']
  25.        [--localpaths='dir1 dir2...'] [--netpaths='dir1 dir2...']
  26.        [--prunepaths='dir1 dir2...'] [--prunefs='fs1 fs2...']
  27.        [--output=dbfile] [--netuser=user] [--localuser=user] 
  28.        [--old-format] [--version] [--help]
  29.  
  30. Report bugs to <bug-findutils@gnu.org>."
  31. changeto=/
  32. old=no
  33. for arg
  34. do
  35.   # If we are unable to fork, the back-tick operator will 
  36.   # fail (and the shell will emit an error message).  When 
  37.   # this happens, we exit with error value 71 (EX_OSERR).
  38.   # Alternative candidate - 75, EX_TEMPFAIL.
  39.   opt=`echo $arg|sed 's/^\([^=]*\).*/\1/'`  || exit 71
  40.   val=`echo $arg|sed 's/^[^=]*=\(.*\)/\1/'` || exit 71
  41.   case "$opt" in
  42.     --findoptions) FINDOPTIONS="$val" ;;
  43.     --localpaths) SEARCHPATHS="$val" ;;
  44.     --netpaths) NETPATHS="$val" ;;
  45.     --prunepaths) PRUNEPATHS="$val" ;;
  46.     --prunefs) PRUNEFS="$val" ;;
  47.     --output) LOCATE_DB="$val" ;;
  48.     --netuser) NETUSER="$val" ;;
  49.     --localuser) LOCALUSER="$val" ;;
  50.     --old-format) old=yes ;;
  51.     --changecwd)  changeto="$val" ;;
  52.     --version) echo "GNU updatedb version 4.2.28"; exit 0 ;;
  53.     --help) echo "$usage"; exit 0 ;;
  54.     *) echo "updatedb: invalid option $opt
  55. $usage" >&2
  56.        exit 1 ;;
  57.   esac
  58. done
  59.  
  60. if test "$old" = yes; then
  61.     echo "Warning: future versions of findutils will shortly discontinue support for the old locate database format." >&2
  62.  
  63.     sort="/usr/bin/sort"
  64.     print_option="-print"
  65.     frcode_options=""
  66. else
  67.     if true
  68.     then
  69.         sort="/usr/bin/sort -z"
  70.         print_option="-print0"
  71.         frcode_options="-0"
  72.     else
  73.         sort="/usr/bin/sort"
  74.         print_option="-print"
  75.         frcode_options=""
  76.     fi
  77. fi
  78.  
  79. getuid() {
  80.     # format of "id" output is ...
  81.     # uid=1(daemon) gid=1(other)
  82.     # for `id's that don't understand -u
  83.     id | cut -d'(' -f 1 | cut -d'=' -f2
  84. }
  85.  
  86. # figure out if su supports the -s option
  87. select_shell() {
  88.     if su "$1" -s $SHELL -c false < /dev/null  ; then
  89.     # No.
  90.     echo ""
  91.     else
  92.     if su "$1" -s $SHELL -c true < /dev/null  ; then
  93.         # Yes.
  94.         echo "-s $SHELL"
  95.         else
  96.         # su is unconditionally failing.  We won't be able to 
  97.         # figure out what is wrong, so be conservative.
  98.         echo ""
  99.     fi
  100.     fi
  101. }
  102.  
  103.  
  104. # You can set these in the environment, or use command-line options,
  105. # to override their defaults:
  106.  
  107. # Any global options for find?
  108. : ${FINDOPTIONS=}
  109.  
  110. # What shell shoud we use?  We should use a POSIX-ish sh.
  111. : ${SHELL="/bin/sh"}
  112.  
  113. # Non-network directories to put in the database.
  114. : ${SEARCHPATHS="/"}
  115.  
  116. # Network (NFS, AFS, RFS, etc.) directories to put in the database.
  117. : ${NETPATHS=}
  118.  
  119. # Directories to not put in the database, which would otherwise be.
  120. : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs /amd /sfs"}
  121.  
  122. # Trailing slashes result in regex items that are never matched, which 
  123. # is not what the user will expect.   Therefore we now reject such 
  124. # constructs.
  125. for p in $PRUNEPATHS; do
  126.     case "$p" in
  127.     /*/)   echo "$0: $p: pruned paths should not contain trailing slashes" >&2
  128.            exit 1
  129.     esac
  130. done
  131.  
  132. # The same, in the form of a regex that find can use.
  133. test -z "$PRUNEREGEX" &&
  134.   PRUNEREGEX=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
  135.  
  136. # The database file to build.
  137. : ${LOCATE_DB=/var/cache/locate/locatedb}
  138.  
  139. # Directory to hold intermediate files.
  140. if test -d /var/tmp; then
  141.   : ${TMPDIR=/var/tmp}
  142. elif test -d /usr/tmp; then
  143.   : ${TMPDIR=/usr/tmp}
  144. else
  145.   : ${TMPDIR=/tmp}
  146. fi
  147. export TMPDIR
  148.  
  149. # The user to search network directories as.
  150. : ${NETUSER=daemon}
  151.  
  152. # The directory containing the subprograms.
  153. if test -n "$LIBEXECDIR" ; then
  154.     : LIBEXECDIR already set, do nothing
  155. else
  156.     : ${LIBEXECDIR=/usr/lib/locate}
  157. fi
  158.  
  159. # The directory containing find.
  160. if test -n "$BINDIR" ; then
  161.     : BINDIR already set, do nothing
  162. else
  163.     : ${BINDIR=/usr/bin}
  164. fi
  165.  
  166. # The names of the utilities to run to build the database.
  167. : ${find:=${BINDIR}/find}
  168. : ${frcode:=${LIBEXECDIR}/frcode}
  169. : ${bigram:=${LIBEXECDIR}/bigram}
  170. : ${code:=${LIBEXECDIR}/code}
  171.  
  172.  
  173. PATH=/bin:/usr/bin:${BINDIR}; export PATH
  174.  
  175. : ${PRUNEFS="nfs NFS proc afs proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs sysfs shfs"}
  176.  
  177. if test -n "$PRUNEFS"; then
  178. prunefs_exp=`echo $PRUNEFS |sed -e 's/\([^ ][^ ]*\)/-o -fstype \1/g' \
  179.  -e 's/-o //' -e 's/$/ -o/'`
  180. else
  181.   prunefs_exp=''
  182. fi
  183.  
  184. # Make and code the file list.
  185. # Sort case insensitively for users' convenience.
  186.  
  187. rm -f $LOCATE_DB.n
  188. trap 'rm -f $LOCATE_DB.n; exit' HUP TERM
  189.  
  190. if test $old = no; then
  191.  
  192. # FIXME figure out how to sort null-terminated strings, and use -print0.
  193. if {
  194. cd "$changeto"
  195. if test -n "$SEARCHPATHS"; then
  196.   if [ "$LOCALUSER" != "" ]; then
  197.     # : A1
  198.     su $LOCALUSER `select_shell $LOCALUSER` -c \
  199.     "$find $SEARCHPATHS $FINDOPTIONS \
  200.      \\( $prunefs_exp \
  201.      -type d -regex '$PRUNEREGEX' \\) -prune -o $print_option"
  202.   else
  203.     # : A2
  204.     $find $SEARCHPATHS $FINDOPTIONS \
  205.      \( $prunefs_exp \
  206.      -type d -regex "$PRUNEREGEX" \) -prune -o $print_option
  207.   fi
  208. fi
  209.  
  210. if test -n "$NETPATHS"; then
  211. myuid=`getuid` 
  212. if [ "$myuid" = 0 ]; then
  213.     # : A3
  214.     su $NETUSER `select_shell $NETUSER` -c \
  215.      "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
  216.     exit $?
  217.   else
  218.     # : A4
  219.     $find $NETPATHS $FINDOPTIONS \( -type d -regex "$PRUNEREGEX" -prune \) -o $print_option ||
  220.     exit $?
  221.   fi
  222. fi
  223. } | $sort -f | $frcode $frcode_options > $LOCATE_DB.n
  224. then
  225.     # OK so far
  226.     true
  227. else
  228.     rv=$?
  229.     echo "Failed to generate $LOCATE_DB.n" >&2
  230.     rm -f $LOCATE_DB.n
  231.     exit $rv
  232. fi
  233.  
  234. # To avoid breaking locate while this script is running, put the
  235. # results in a temp file, then rename it atomically.
  236. if test -s $LOCATE_DB.n; then
  237.   rm -f $LOCATE_DB
  238.   mv $LOCATE_DB.n $LOCATE_DB
  239.   chmod 644 $LOCATE_DB
  240. else
  241.   echo "updatedb: new database would be empty" >&2
  242.   rm -f $LOCATE_DB.n
  243. fi
  244.  
  245. else # old
  246.  
  247. if ! bigrams=`mktemp -t updatedbXXXXXXXXX`; then
  248.     echo tempfile failed
  249.     exit 1
  250. fi
  251.  
  252. if ! filelist=`mktemp -t updatedbXXXXXXXXX`; then
  253.     echo tempfile failed
  254.     exit 1
  255. fi
  256.  
  257. rm -f $LOCATE_DB.n
  258. trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' HUP TERM
  259.  
  260. # Alphabetize subdirectories before file entries using tr.  James Woods says:
  261. # "to get everything in monotonic collating sequence, to avoid some
  262. # breakage i'll have to think about."
  263. {
  264. cd "$changeto"
  265. if test -n "$SEARCHPATHS"; then
  266.   if [ "$LOCALUSER" != "" ]; then
  267.     # : A5
  268.     su $LOCALUSER `select_shell $LOCALUSER` -c \
  269.     "$find $SEARCHPATHS $FINDOPTIONS \
  270.      \( $prunefs_exp \
  271.      -type d -regex '$PRUNEREGEX' \) -prune -o $print_option" || exit $?
  272.   else
  273.     # : A6
  274.     $find $SEARCHPATHS $FINDOPTIONS \
  275.      \( $prunefs_exp \
  276.      -type d -regex "$PRUNEREGEX" \) -prune -o $print_option || exit $?
  277.   fi
  278. fi
  279.  
  280. if test -n "$NETPATHS"; then
  281.   myuid=`getuid`
  282.   if [ "$myuid" = 0 ]; then
  283.     # : A7
  284.     su $NETUSER `select_shell $NETUSER` -c \
  285.      "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
  286.     exit $?
  287.   else
  288.     # : A8
  289.     $find $NETPATHS $FINDOPTIONS \( -type d -regex "$PRUNEREGEX" -prune \) -o $print_option ||
  290.     exit $?
  291.   fi
  292. fi
  293. } | tr / '\001' | $sort -f | tr '\001' / > $filelist
  294.  
  295. # Compute the (at most 128) most common bigrams in the file list.
  296. $bigram $bigram_opts < $filelist | sort | uniq -c | sort -nr |
  297.   awk '{ if (NR <= 128) print $2 }' | tr -d '\012' > $bigrams
  298.  
  299. # Code the file list.
  300. $code $bigrams < $filelist > $LOCATE_DB.n
  301.  
  302. rm -f $bigrams $filelist
  303.  
  304. # To reduce the chances of breaking locate while this script is running,
  305. # put the results in a temp file, then rename it atomically.
  306. if test -s $LOCATE_DB.n; then
  307.   rm -f $LOCATE_DB
  308.   mv $LOCATE_DB.n $LOCATE_DB
  309.   chmod 644 $LOCATE_DB
  310. else
  311.   echo "updatedb: new database would be empty" >&2
  312.   rm -f $LOCATE_DB.n
  313. fi
  314.  
  315. fi
  316.  
  317. exit 0
  318.